package com.lb.jwitter.hibernate;

import org.apache.commons.logging.Log;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import com.lb.utils.orm.hibernate.HibernateGenericDao;
import org.springframework.transaction.annotation.Transactional;
import com.lb.jwitter.Tbl_CTDT_DM_TietHoc;

@Repository("tbl_CTDT_DM_TietHocDAO")
public class Tbl_CTDT_DM_TietHocHibernateDAO extends HibernateGenericDao<Tbl_CTDT_DM_TietHoc, Long> implements Tbl_CTDT_DM_TietHocDAO {
   private Log log = LogFactory.getLog(Tbl_CTDT_DM_TietHocHibernateDAO.class);

   @Autowired
   public Tbl_CTDT_DM_TietHocHibernateDAO(@Qualifier("sessionFactory")   SessionFactory sessionFactory) {
       super(sessionFactory);
   }

   @SuppressWarnings("unchecked")
@Transactional
   public final List<Tbl_CTDT_DM_TietHoc> findAll(int pageSize, int page) {
       if( pageSize == 0) return getSession().createCriteria(Tbl_CTDT_DM_TietHoc.class).list(); 
       return getSession().createCriteria(Tbl_CTDT_DM_TietHoc.class).setFirstResult(pageSize * (page-1)).setMaxResults(pageSize).list();
   }
   
@Transactional
   public final Long save(Tbl_CTDT_DM_TietHoc _newInstance) {
   	return super.save(_newInstance);
   }

@Transactional
   public final boolean exists(Long id) {
       boolean result = (Integer) getSession().createCriteria(Tbl_CTDT_DM_TietHoc.class).setProjection(Projections.rowCount()).add(
                       Restrictions.eq("id", id)).uniqueResult() == 1;
       log.info("Result: "+result);
   	return result;
   }

@Transactional
   public Collection<Tbl_CTDT_DM_TietHoc> find(Tbl_CTDT_DM_TietHoc obj,int pageSize,int page){
   	Collection<Tbl_CTDT_DM_TietHoc> _tbl_CTDT_DM_TietHocs = new ArrayList<Tbl_CTDT_DM_TietHoc>();
   	try {
   		String sql = "select d from tbl_CTDT_DM_TietHoc as d where 1=1 ";
   sql += " and d.id = " + obj.getId() + " ";
       if(obj.getTen() != null && !obj.getTen().equalsIgnoreCase("")){
   		sql +=  " and d.ten like '%" +obj.getTen() + "%' " ;
   }
       if(obj.getMa() != null && !obj.getMa().equalsIgnoreCase("")){
   		sql +=  " and d.ma like '%" +obj.getMa() + "%' " ;
   }
       if(obj.getThoiGianBatDau() != null && !obj.getThoiGianBatDau().equalsIgnoreCase("")){
   		sql +=  " and d.thoiGianBatDau like '%" +obj.getThoiGianBatDau() + "%' " ;
   }
   sql += " and d.ThoiGianKetThuc = " + obj.getThoiGianKetThuc() + " ";
   sql += " and d.sTT = " + obj.getSTT() + " ";
   		sql += " order by d.id DESC ";
   		_tbl_CTDT_DM_TietHocs = getSession().createQuery(sql).setFirstResult(pageSize*(page-1)).setMaxResults(pageSize).list();
   	} catch (Exception e) {
   		e.printStackTrace();
   	}
   	return _tbl_CTDT_DM_TietHocs;
   }

}//endclass
